package com.xinchao.filter;


import com.xinchao.constant.RequestConstant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录过滤器
 *
 * @author dxy
 * @date 2018/7/5 12:39
 */
public class LoginFilter implements Filter {
	private static final String LOGIN = "/web/login";

	private static final String DOLOGIN = "/web/doLogin";

	private static final String LOGOUT = "/web/logout";
	
	private static final String EXT = "/ext";
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		//上下文路径
		String contextPath = httpRequest.getContextPath();
		String requestURI = httpRequest.getRequestURI();
		//放行静态资源
		if (requestURI.endsWith(".js") || requestURI.endsWith(".css") || requestURI.endsWith(".img") || requestURI.endsWith(".png")) {
			chain.doFilter(httpRequest, httpResponse);
			return;
		}
		//goLogin、login,直接放行
		if (requestURI.equals(contextPath + LOGIN) || requestURI.equals(contextPath + DOLOGIN) || requestURI.equals(contextPath + LOGOUT)) {
			chain.doFilter(httpRequest, httpResponse);
			return;
		}
		// TODO 无法穷举URI,故无法获取更多的监控信息，spring-boot-admin监控请求,直接放行
		if ( isMonitorUrl(requestURI, contextPath) ) {
			chain.doFilter(httpRequest, httpResponse);
			return;
		}
		// 对外提供接口放行
		if ( requestURI.startsWith(contextPath + EXT) ) {
			chain.doFilter(httpRequest, httpResponse);
			return;
		}
		//验证用户是否登录
		Object user = httpRequest.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		if (user == null) {
			httpResponse.sendRedirect(contextPath + LOGIN);
			return;
		} else {
			chain.doFilter(httpRequest, httpResponse);
			return;
		}
	}

	@Override
	public void destroy() {

	}
	
	private boolean isMonitorUrl(String requestURI,String contextPath){
		if (requestURI.startsWith(contextPath + "/health")
				|| requestURI.startsWith(contextPath + "/status")
				|| requestURI.startsWith(contextPath + "/actuator")
				|| requestURI.startsWith(contextPath + "/info")
				|| requestURI.startsWith(contextPath + "/env")
				|| requestURI.startsWith(contextPath + "/metrics")
				|| requestURI.startsWith(contextPath + "/httptrace")
				|| requestURI.startsWith(contextPath + "/auditevents")
				|| requestURI.startsWith(contextPath + "/threaddump")
				|| requestURI.startsWith(contextPath + "/heapdump")
				|| requestURI.startsWith(contextPath + "/jolokia")
				|| requestURI.startsWith(contextPath + "/auditevents")
				|| requestURI.startsWith(contextPath + "/beans")
				|| requestURI.startsWith(contextPath + "/conditions")
				|| requestURI.startsWith(contextPath + "/configprops")
				|| requestURI.startsWith(contextPath + "/scheduledtasks")
				|| requestURI.startsWith(contextPath + "/mappings")
				|| requestURI.startsWith(contextPath + "/loggers")) {
			return true;
		}
		return false;
	}
}
